﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Entity;
using Job.Logic.Company;

namespace Job.Web.userControls.Company
{
    public partial class Introduction : BaseCtrl
    {
        private int CompanyId
        {
            get
            {
                //后台管理员能通过后台为企业增加职位，所以在此处需要兼职企业ID从url或从Session中获取
                int id = myR["company"].IntValue;
                if (id > 0) return id;
                MySession session = new MySession();
                SessionCompany sCompany = session.GetCompanyFromSession();
                if (null == sCompany)
                    return 0;
                else
                    return sCompany.CompanyId;
            }
        }

        CompanyLogic logic = new CompanyLogic();

        private int subjectId
        {
            get { return myR["subjectid"].IntValue; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.message.Visible = false;
            
            if (0 == CompanyId)
            {
                Response.Redirect("CompanyLogin.aspx?categoryid=2&error=1");
                return;
            }

            if (!this.IsPostBack)
            {
                switch (subjectId)
                {
                    case (int)Constants.CompanySubject.ProductIntro:
                        this.title.InnerText = "产品介绍";
                        break;
                    default:
                        this.title.InnerText = "公司介绍";
                        break;
                }
                
                LoadIntro();
            }
        }

        private bool LoadIntro()
        {
            JobCompanyInfo info = logic.GetCompanyInfo(CompanyId);
            if (null == info)
            {
                this.message.InnerText = "系统内部异常，查询不到企业资料！";
                this.message.Visible = true;
                Response.Redirect("CompanyLogin.aspx?categoryid=2&error=2");
                return false;
            }

            switch (subjectId)
            {
                case (int)Constants.CompanySubject.ProductIntro:
                    this.tbMemo.Text = info.ProductIntro;
                    break;
                default:
                    this.tbMemo.Text = info.CompanyIntro;
                    break;
            }            
            return true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                switch (subjectId)
                {
                    case (int)Constants.CompanySubject.ProductIntro:
                        logic.UpdateProductIntro(CompanyId, tbMemo.Text.Trim());                      
                        break;
                    default:                        
                        logic.UpdateCompanyIntro(CompanyId, tbMemo.Text.Trim());
                        break;
                }
                this.message.InnerHtml = "<font color='#990000'>保存资料成功！</font>";      
            }
            catch (Exception ex)
            {
                 this.message.InnerHtml = "<font color='#ff0000'>系统内部异常，保存失败：" + ex.Message + "</font>";
            }
            this.message.Visible = true;
            this.message.Height = "30px";
        }


    }
}